package router

import (
	"crow-samba/app/service"
	"github.com/gin-gonic/gin"
)

// AddDirectories 创建共享目录
// @Summary 创建共享目录
// @Description 创建共享目录
// @Tags directory
// @Accept  application/json
// @Product application/json
// @Param data body []service.Directory  true "共享名，目录绝对路径"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory [post]
// @Security Bearer
func AddDirectories(c *gin.Context) {
	var dirList []service.Directory
	err := c.Bind(&dirList)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	o := service.Officer{
		DirectoryList: dirList,
	}
	err = o.AddDirectories()
	//o.DirList2Config()
	//_, err = o.Config.WriteConf()
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "添加成功")
}

// GetDirectoryList 获取目录列表
// @Summary 获取目录列表
// @Description 获取目录列表
// @Tags directory
// @Success 200 {object} response.Response{data=[]service.Directory} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory/List [get]
// @Security Bearer
func GetDirectoryList(c *gin.Context) {
	var dirList []service.Directory
	o := &service.Officer{
		DirectoryList: dirList,
	}
	err := o.GetDirectoryList()
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, o.DirectoryList)
}

// GetDirectory 获取指定目录信息
// @Summary 获取指定目录信息
// @Description 获取指定目录信息
// @Tags directory
// @Param dir_tag query string false "目录标签"
// @Success 200 {object} response.Response{data=service.Directory} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory [get]
// @Security Bearer
func GetDirectory(c *gin.Context) {
	dirTag := c.Query("dir_tag")
	o := &service.Officer{}
	dir, err := o.GetDirectory(dirTag)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, dir)
}

// SetDirectoryWriter 重置写用户
// @Summary 重置写用户
// @Description 重置写用户
// @Tags directory
// @Accept  application/json
// @Product application/json
// @Param data body service.Directory  true "必传：dirTag,writerList.无效：Path"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory/writers [put]
// @Security Bearer
func SetDirectoryWriter(c *gin.Context) {
	//var d DirectoryWriter
	var d service.Directory
	err := c.Bind(&d)
	if err != nil {
		SetErrDefault(c, err)
		return
	}

	o := service.Officer{
		DirectoryList: []service.Directory{d},
	}
	err = o.SetConfigPartWriters()
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "添加成功")
}

// RemoveWriterFromDirectory 移除写用户
// @Summary 移除写用户
// @Description 移除写用户
// @Tags directory
// @Param dir_tag path string true "目录标签"
// @Param user_list query string true "用户名列表，用逗号分开"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory/{dir_tag}/writers [delete]
// @Security Bearer
func RemoveWriterFromDirectory(c *gin.Context) {

	dirTag := c.Param("dir_tag")
	userList := c.Query("user_list")
	//实例化目录
	d := &service.Directory{
		DirTag: dirTag,
	}
	o := &service.Officer{
		DirectoryList: []service.Directory{*d},
	}

	//从该目录移除用户
	err := o.RemoveWriters(userList)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}

// AddWriterToDirectory 添加写用户
// @Summary 添加写用户
// @Description 添加写用户
// @Tags directory
// @Accept  application/json
// @Product application/json
// @Param data body DirectoryWriter  true "目录标签，用户名"
// @Success 200 {object} response.Response{data=[]string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory/writers [post]
// @Security Bearer
func AddWriterToDirectory(c *gin.Context) {
	var r DirectoryWriter
	err := c.Bind(&r)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	d := &service.Directory{
		DirTag: r.DirTag,
	}
	o := &service.Officer{
		DirectoryList: []service.Directory{*d},
	}

	newWriterList, err := o.AddWriter(r.UserNameList)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, newWriterList)
}

// DeleteDirectory 取消共享目录
// @Summary 取消共享目录
// @Description 取消共享目录（不会删除实际目录）
// @Tags directory
// @Param dir_tag path string true "目录标签"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/directory/{dir_tag} [delete]
// @Security Bearer
func DeleteDirectory(c *gin.Context) {
	dirTag := c.Param("dir_tag")
	o := &service.Officer{}
	//从该目录移除用户
	err := o.DeletePart(dirTag)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}
